package com.wl.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wl.dao.AdminMapper;
import com.wl.entity.Admin;
import com.wl.entity.Emp;
import com.wl.service.AdminService;
import com.wl.util.MD5Util;
import com.wl.util.PageData;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper am;

	@Override
	public PageData<Admin> getAdminByPage(int page, int limit, String content) {
		// 业务判断
		int start = (page - 1) * limit;
		// 查询需要返回的核心数据[指定页码的]
		List<Admin> data = am.selectByPage(start, limit, content);
		int count = am.selectCount(content);
		PageData<Admin> pd = new PageData<>(data, count, limit, page);
		return pd;
	}

	@Override
	public boolean addAdmin(Admin admin) {
		// TODO Auto-generated method stub
		if (admin == null) {
			return false;
		} else {
			admin.setAdminpwd(MD5Util.getEncodeByMd5(admin.getAdminpwd()));
			System.out.println("AdminService: " + admin);
			return am.insert(admin) > 0;
		}
	}

	@Override
	public boolean updateAdmin(Admin admin) {
		// TODO Auto-generated method stub
		return am.updateByPrimaryKeySelective(admin) > 0;
	}

	@Override
	public Admin login(Admin admin) {
		// TODO Auto-generated method stub
		if (admin == null) {
			return null;
		} else {
			admin.setAdminpwd(MD5Util.getEncodeByMd5(admin.getAdminpwd()));
			// return am.selectAllAdmin();
		}
		// 注意了不要拿去直接用
		return null;
	}

	@Override
	public boolean delAdminById(int adminId) {
		// TODO Auto-generated method stub
		return am.deleteByPrimaryKey(adminId) > 0;
	}

	@Override
	public boolean checkLoginByAdmin(String adminname, String adminpwd) {
		// TODO Auto-generated method stub
		Admin admin = am.findByNameAndPwd(adminname, adminpwd);
		boolean flag = false;
		if (admin!=null) {
			flag = true;
		}else {
			flag = false;
		}
		return flag;
	}

}
